import datetime,time
from .setting import *
from peewee import *


def getdb():
    if DBTYPE == 'mysql':
        return MySQLDatabase(DBNAME, host=HOST, user=USER, passwd=PASSWD, port=PORT)
    elif DBTYPE == 'sqlite':
        return SqliteDatabase(SQLITEDBNAME)

class basemodel(Model):
    class Meta:
        database = getdb()

class peaktask(basemodel):
    id = PrimaryKeyField( unique=True )
    name = CharField( null = False )
    title = CharField(null = False)
    content = TextField( null = False )
    sign = CharField( null = False )
    interval = IntegerField(null=False,default=60)
    nexttime = DateTimeField(null=False)
    status = CharField(null=False,default='')
    remark = CharField(null = False,default='')

    def init(self):
        peaktask.create_table()

    def exist(self,sign):
        try:
            return not peaktask.get(peaktask.sign==sign) == None
        except Exception as ex:
            return False


class peaktasklog(basemodel):
    id = PrimaryKeyField( unique=True )
    taskid = IntegerField(null = False)
    addtime = DateTimeField( null = False )
    status = CharField( null = False )
    log = CharField( null = False )

    def init(self):
        peaktasklog.create_table()
